package com.demo.security.config;

import com.demo.security.filter.AppUsernamePasswordAuthenticationFilter;
import com.demo.security.handler.AppAccessDeniedHandler;
import com.demo.security.handler.AppAuthenticationFailureHandler;
import com.demo.security.handler.AppAuthenticationSuccessHandler;
import com.demo.security.provider.AppAuthenticationProvider;
import com.demo.security.service.AppUserDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

@Configuration
@EnableWebSecurity
// 开启注解权限控制
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class AppSecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    AppAuthenticationProvider appAuthenticationProvider;

    @Autowired
    AppAuthenticationSuccessHandler appAuthenticationSuccessHandler;

    @Autowired
    AppAuthenticationFailureHandler appAuthenticationFailureHandler;

    @Autowired
    AppAccessDeniedHandler appAccessDeniedHandler;

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                .addFilterAt(getLoginFilter(), UsernamePasswordAuthenticationFilter.class)
                .csrf().disable()
                .exceptionHandling()
                .accessDeniedHandler(appAccessDeniedHandler)
                .and()
                .authorizeRequests()
//                .antMatchers().permitAll()
                .anyRequest().authenticated()
                .and()
                .formLogin().loginPage("/mylogin.html").permitAll()
                .and()
                .logout().logoutUrl("/logout").permitAll();
    }

    @Override
    public void configure(WebSecurity web) throws Exception {
        //不进行保护的资源
        web.ignoring().antMatchers("/css/**", "/js/**");
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.authenticationProvider(appAuthenticationProvider);
    }

    private AppUsernamePasswordAuthenticationFilter getLoginFilter() throws Exception{
        AppUsernamePasswordAuthenticationFilter appUsernamePasswordAuthenticationFilter = new AppUsernamePasswordAuthenticationFilter("/login");
        appUsernamePasswordAuthenticationFilter.setAuthenticationSuccessHandler(appAuthenticationSuccessHandler);
        appUsernamePasswordAuthenticationFilter.setAuthenticationFailureHandler(appAuthenticationFailureHandler);
        appUsernamePasswordAuthenticationFilter.setAuthenticationManager(this.authenticationManager());
        return appUsernamePasswordAuthenticationFilter;
    }
}
